﻿using Heima.OA.Common;
using Heima.OA.IBLL;
using Heima.OA.Model;
using Heima.OA.UI.Portal.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Heima.OA.UI.Portal.Areas.Declare.Controllers
{
    public class HouseInfoController : BaseController
    {
        public IZS_HouseInfoService ZS_HouseInfoService { get; set; }
        public IZS_BuildingInfoService ZS_BuildingInfoService { get; set; }
        // GET: Declare/HouseInfo
        public string Index(PaginationQuery paginationQuery)
        {
            int total = 0;
            var houseList = ZS_HouseInfoService.GetPageEntities(paginationQuery.pagesize, paginationQuery.pageindex, out total, u => u.DelFlag == delflagNormal,
                s => s.CreateTime, true).ToList();
            List<object> objList = new List<object>();
            foreach (var house in houseList)
            {
                ZS_BuildingInfo building = ZS_BuildingInfoService.GetModel(house.BuildingId ?? 0);
                objList.Add(new
                {
                    house.Id,
                    house.BuildingId,
                    BuildingName = building != null ? building.BuildingName : "",
                    house.HouseName,
                    house.HouseArea,
                    house.DecorationSituation,
                    house.ActualityUse,
                    house.Remark,
                    house.DelFlag,
                    house.CreateTime,
                    house.DeclareId
                });
            }
            PaginationData data = new PaginationData() { total = total, rows = JsonHandler.ToJson(objList) };
            JsonMessage msg = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "", JsonHandler.ToJson(data));

            return JsonHandler.ToJson(msg);
        }

        #region 添加
        public string Create(ZS_HouseInfo houseInfo)
        {
            houseInfo.DelFlag = delflagNormal;
            houseInfo.CreateTime = DateTime.Now;

            ZS_HouseInfoService.Add(houseInfo);
            JsonMessage message = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "添加成功!");
            return JsonHandler.ToJson(message);
        }

        #endregion
        #region 编辑
        public string Edit(ZS_HouseInfo houseInfo)
        {
            ZS_HouseInfo editHouse = ZS_HouseInfoService.GetModel(houseInfo.Id);
            string res = string.Empty;
            if (editHouse == null)
            {
                res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "编辑错误!"));
            }
            else
            {
                editHouse.BuildingId = houseInfo.BuildingId;
                editHouse.HouseName = houseInfo.HouseName;
                editHouse.HouseArea = houseInfo.HouseArea;
                editHouse.DecorationSituation = houseInfo.DecorationSituation;
                editHouse.ActualityUse = houseInfo.ActualityUse;
                editHouse.Remark = houseInfo.Remark;

                bool success = ZS_HouseInfoService.Update(editHouse);
                if (success)
                {
                    res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "编辑成功!"));
                }
            }

            return res;
        }
        #endregion
        #region 删除
        public string Delete(int id)
        {
            ZS_HouseInfo houseInfo = ZS_HouseInfoService.GetModel(id);
            string res = string.Empty;
            if (houseInfo == null)
            {
                res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "删除错误!"));
            }
            else
            {
                houseInfo.DelFlag = (int)DelFlagEnum.Deleted;
                bool success = ZS_HouseInfoService.Update(houseInfo);
                if (success)
                {
                    res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "删除成功!"));
                }
            }
            return res;
        }
        #endregion

    }
}